﻿Public Class formusereditor

    Private Sub formusereditor_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        cbouser.DisplayMember = "EMP_NAME"
        cbouser.ValueMember = "EMP_ID"
        cbouser.DataSource = CMD.ExecuteDataTable("select * from TBL_EMPLOYEE")
        If txtid.Text.Trim = "" Then
            txtl_name.Clear()
            txtpwd.Clear()
            txtrpwd.Clear()
            rduser.Checked = True
        Else
            txtpwd.Enabled = False
            txtrpwd.Enabled = False
            CMD.Parameters("@id") = txtid.Text
            Dim dr = CMD.ExecuteDataReader("select * from TBL_USER where U_ID=@id")
            While dr.Read
                cbouser.SelectedValue = dr(1)
                txtl_name.Text = dr(2)
                If dr(3) = "Admin" Then
                    rdamin.Checked = True
                Else
                    rduser.Checked = True
                End If
                txtpwd.Text = dr(4)
                txtrpwd.Text = dr(4)
            End While
            dr.Close()
        End If
    End Sub

    Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles txtrpwd.TextChanged

    End Sub

    Private Sub ButtonX2_Click(sender As Object, e As EventArgs) Handles ButtonX2.Click
        If txtl_name.Text.Trim = "" Then
            MsgBox("Please input Your Log in Name.......!")
            Exit Sub
        End If
        If txtpwd.Text.Trim = "" Then
            MsgBox("Pleae Input Your Password....!")
            Exit Sub
        End If
        If txtrpwd.Text.Trim = "" Then
            MsgBox("Please confirm Your Password ...!")
            Exit Sub
        End If
        If txtid.Text.Trim = "" Then
            If txtpwd.Text.ToString = txtrpwd.Text.ToString Then
                CMD.Parameters("@e_id") = cbouser.SelectedValue
                Dim count = CMD.ExecuteScalar("SELECT count(*) FROM TBL_USER WHERE EMP_ID=@e_id ")
                If count > 0 Then
                    MsgBox("This employee have already account....!")
                    cbouser.SelectAll()
                Else
                    CMD.Parameters("@name") = txtl_name.Text.ToString
                    CMD.Parameters("@pwd") = txtpwd.Text.ToString
                    Dim countuser = CMD.ExecuteScalar("SELECT count(*) FROM TBL_USER WHERE LOG_NAME=@name AND PWD=@pwd ")
                    If countuser > 0 Then
                        MsgBox("This Username and passord already exist Please choose other name or password....!")
                        txtl_name.SelectAll()
                    Else
                        Dim per As String
                        If rdamin.Checked = True Then
                            per = "Admin"
                        Else
                            per = "User"
                        End If
                        CMD.Parameters("@e_id") = cbouser.SelectedValue
                        CMD.Parameters("@name") = txtl_name.Text.ToString
                        CMD.Parameters("@pwd") = txtpwd.Text.ToString
                        CMD.Parameters("@permis") = per.Trim
                        CMD.ExecuteNonQuery("INSERT INTO TBL_USER VALUES(@e_id,@name,@permis,@pwd)")
                        MsgBox("New User Has been Add.....!")
                        Form1.loaduser()
                    End If
                End If

            Else
                MsgBox("Please confirm Your password again....!")
                txtrpwd.SelectAll()
            End If
        Else
                CMD.Parameters("@id") = txtid.Text
                Dim per As String
                If rdamin.Checked = True Then
                    per = "Admin"
                Else
                    per = "User"
                End If
                CMD.Parameters("@e_id") = cbouser.SelectedValue
                CMD.Parameters("@permis") = per.Trim
                CMD.Parameters("@name") = txtl_name.Text.ToString
                CMD.ExecuteNonQuery("UPDATE TBL_USER SET EMP_ID=@e_id,LOG_NAME=@name,U_PERMIS=@permis WHERE U_ID=@id")
                MsgBox(" User Has been Update.....!")
                Form1.loaduser()
            End If

    End Sub
End Class